package com.lanou.controller;

import com.google.gson.Gson;
import com.lanou.pojo.Tb_user;
import com.lanou.service.InsertService;
import com.lanou.utils.AjaxResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "RegisterServlet",urlPatterns = "/register")
public class RegisterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
    private InsertService insertService = new InsertService();
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String address = request.getParameter("address");
        String gender = request.getParameter("gender");
        String phone = request.getParameter("phone");
        response.setContentType("application/json;charset=utf-8");
        AjaxResult ar = new AjaxResult();
        PrintWriter out = response.getWriter();

        Tb_user tbUser = new Tb_user();
        tbUser.setUsername(username);
        tbUser.setPassword(password);
        tbUser.setEmail(email);
        tbUser.setAddress(address);
        tbUser.setGender(gender);
        tbUser.setPhone(phone);
        tbUser.setState(1);
        int insert = insertService.insertUser(tbUser);
        if (insert == 1000){
            ar.setCode(insert);
            ar.setMsg("注册成功");
        }else {
            ar.setCode(insert);
            ar.setMsg("注册失败");
        }
        out.write(new Gson().toJson(ar));
        out.close();
    }
}
